package bancosys.tec.persist.hibernate.eventListener;

import org.hibernate.event.EventSource;
import org.hibernate.event.FlushEvent;
import org.hibernate.event.FlushEventListener;

/**
 * Defines the default flush event listeners used by hibernate for flushing session state in response to generated flush events.
 * 
 * @author Cesar Olmos
 */
public class DirtyAwareFlushEventListener extends AbstractDirtyAwareFlushEventListener implements FlushEventListener {

    /**
     * Handle the given flush event.
     * 
     * @param event The flush event to be handled.
     */
    public void onFlush(FlushEvent event) {
        final EventSource source = event.getSession();
        if (source.getPersistenceContext().hasNonReadOnlyEntities()) {

            this.flushEverythingToExecutions(event);
            this.performExecutions(source);
            this.postFlush(source);

            if (source.getFactory().getStatistics().isStatisticsEnabled()) {
                source.getFactory().getStatisticsImplementor().flush();
            }

        }
    }
}
